package com.zhiyou100.blog.aciton.user;

import com.zhiyou100.blog.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashSet;

/*
作者：苗天宝
班级：java37
班训：天道酬勤 不忘初衷
时间：2021年10月22日 下午2:30:55
*/
@WebServlet("/user/logout")
public class UserLogoutServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getSession().invalidate(); //使所有session失效
        HttpSession session = req.getSession();

        HashSet applicationSet = (HashSet) session.getAttribute("applicationSet");
        User loginUser = (User) session.getAttribute("loginUser");
        applicationSet.remove(loginUser.getUname());
        System.out.println("applicationSet = " + applicationSet.size());

        session.removeAttribute("loginUser"); //删除登陆者
        req.setAttribute("message", "登出成功！");
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
